package com.feixiang.springbootinit.strategy;

import cn.hutool.json.JSONUtil;
import com.feixiang.springbootinit.common.ErrorCode;
import com.feixiang.springbootinit.exception.BusinessException;
import com.feixiang.springbootinit.model.entity.wechat.QwMessage;
import com.feixiang.springbootinit.model.entity.wechat.QwTextMessage;
import com.feixiang.springbootinit.model.entity.wechat.QwVideoMessage;
import com.feixiang.springbootinit.model.enums.SessionArchiveMsgTypeEnum;
import com.feixiang.springbootinit.model.vo.wechat.MsgVO;
import com.feixiang.springbootinit.processor.SessionArchiveBatchProcessor;
import com.feixiang.springbootinit.service.QwTextMessageService;
import com.feixiang.springbootinit.utils.BatchSaveUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class TextMessageStrategy implements MessageProcessingStrategy {

    @Autowired
    private QwTextMessageService textMessageService;

    @Override
    public boolean supports(SessionArchiveMsgTypeEnum msgType) {
        return SessionArchiveMsgTypeEnum.TEXT == msgType;
    }

    @Override
    public void process(MsgVO msgVO, QwMessage baseMessage) {
        log.info("处理文本消息");
        try {
            QwTextMessage textMessage = new QwTextMessage();
            textMessage.setMsgId(msgVO.getMsgid());
            textMessage.setContent(msgVO.getText().getContent());
            textMessage.setCreatedTime(new Date());
            textMessageService.save(textMessage);
            log.info("保存文本消息成功,消息id:{}", textMessage.getMsgId());
        } catch (Exception e) {
            log.error("处理文本消息失败", e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "处理文本消息失败");
        }
    }

    @Override
    public void batchProcess(List<QwMessage> baseMessages) {
        Date currentTime = new Date();
        if (baseMessages.isEmpty()) {
            log.info("批量处理视频消息: 空列表，无需处理");
            return;
        }
        log.info("开始批量处理文本消息，数量: {}", baseMessages.size());
        List<QwTextMessage> textMessages = new ArrayList<>(baseMessages.size());
        //需取出来进行转换
        for (QwMessage baseMsg : baseMessages) {
            try {
                MsgVO msgVO = JSONUtil.toBean(baseMsg.getContent(), MsgVO.class);
                QwTextMessage textMessage = new QwTextMessage();
                textMessage.setMsgId(msgVO.getMsgid());
                textMessage.setContent(msgVO.getText().getContent());
                textMessage.setCreatedTime(currentTime);
                textMessages.add(textMessage);
            } catch (Exception e) {
                log.error("批量处理单条文字消息失败, msgId: {}", baseMsg.getMsgId(), e);
                // 单条失败不影响整体批次，继续处理其他消息
            }
        }
        // 批量保存到数据库
        BatchSaveUtil.batchSave(textMessages, textMessageService::insertBatch, "文字消息");
//        if (!textMessages.isEmpty()) {
//            try {
//                // 分批次保存，每批最多500条
//                int batchSize = 500;
//                for (int i = 0; i < textMessages.size(); i += batchSize) {
//                    int end = Math.min(i + batchSize, textMessages.size());
//                    List<QwTextMessage> subList = textMessages.subList(i, end);
//                    textMessageService.saveBatch(subList);
//                }
//                log.info("成功保存 {} 条文字消息", textMessages.size());
//            } catch (Exception e) {
//                log.error("批量保存文字消息失败", e);
//                throw new BusinessException(ErrorCode.OPERATION_ERROR, "批量保存文字消息失败");
//            }
//        }
    }
}
